package com.macro.mall.oms.service.order.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.macro.mall.common.api.exception.BusinessException;
import com.macro.mall.oms.domain.OrderItem;
import com.macro.mall.oms.entity.OrderItemEntity;
import com.macro.mall.oms.mapper.OrderItemMapper;
import com.macro.mall.oms.service.order.OrderItemService;
import com.macro.mall.oms.service.order.bo.InsertOrderItemInput;
import com.macro.mall.oms.service.order.bo.PagedQueryOrderItemInput;
import com.macro.mall.oms.service.order.bo.UpdateOrderItemInput;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 订单明细Service
 *
 * @author xuyanjun
 * @date 2019-12-10
 */
@Slf4j
@Service
public class OrderItemServiceImpl implements OrderItemService {

    @Autowired
    private OrderItemMapper orderItemMapper;

    /**
     * 新增订单明细
     *
     * @param input input
     * @return 新增id
     */
    @Override
    public long insert(InsertOrderItemInput input) {
        OrderItemEntity orderItemEntity = new OrderItemEntity();
        orderItemEntity
                .setOrderId(input.getOrderId())
                .setOrderSn(input.getOrderSn())
                .setProductId(input.getProductId())
                .setProductPic(input.getProductPic())
                .setProductName(input.getProductName())
                .setProductBrand(input.getProductBrand())
                .setProductSn(input.getProductSn())
                .setProductPrice(input.getProductPrice())
                .setProductQuantity(input.getProductQuantity())
                .setProductSkuId(input.getProductSkuId())
                .setProductSkuCode(input.getProductSkuCode())
                .setProductCategoryId(input.getProductCategoryId())
                .setSp1(input.getSp1())
                .setSp2(input.getSp2())
                .setSp3(input.getSp3())
                .setPromotionAmount(BigDecimal.ZERO)
                .setCouponAmount(BigDecimal.ZERO)
                .setIntegrationAmount(BigDecimal.ZERO)
                .setRealAmount(input.getRealAmount())
                .setGiftIntegration(0)
                .setGiftGrowth(0)
                .setProductAttr(input.getProductAttr())
                .setMemberId(input.getMemberId());

        orderItemMapper.insert(orderItemEntity);
        return orderItemEntity.getId();
    }

    /**
     * 修改订单明细
     *
     * @param input input
     */
    @Override
    public void update(UpdateOrderItemInput input) {
        OrderItemEntity orderItemEntity = orderItemMapper.selectById(input.getId());
        if (orderItemEntity == null) {
            throw new BusinessException(String.format("未能根据Id:%s找到指定的订单明细。", input.getId()));
        }

        orderItemEntity = new OrderItemEntity();
        orderItemEntity.setId(input.getId())
                .setOrderId(input.getOrderId())
                .setOrderSn(input.getOrderSn())
                .setProductId(input.getProductId())
                .setProductPic(input.getProductPic())
                .setProductName(input.getProductName())
                .setProductBrand(input.getProductBrand())
                .setProductSn(input.getProductSn())
                .setProductPrice(input.getProductPrice())
                .setProductQuantity(input.getProductQuantity())
                .setProductSkuId(input.getProductSkuId())
                .setProductSkuCode(input.getProductSkuCode())
                .setProductCategoryId(input.getProductCategoryId())
                .setSp1(input.getSp1())
                .setSp2(input.getSp2())
                .setSp3(input.getSp3())
                .setPromotionName(input.getPromotionName())
                .setPromotionAmount(input.getPromotionAmount())
                .setCouponAmount(input.getCouponAmount())
                .setIntegrationAmount(input.getIntegrationAmount())
                .setRealAmount(input.getRealAmount())
                .setGiftIntegration(input.getGiftIntegration())
                .setGiftGrowth(input.getGiftGrowth())
                .setProductAttr(input.getProductAttr());

        orderItemMapper.updateById(orderItemEntity);
    }

    /**
     * 删除订单明细
     *
     * @param id id
     */
    @Override
    public void delete(long id) {
        orderItemMapper.deleteById(id);
    }

    /**
     * 根据Id获取订单明细
     *
     * @param id id
     * @return 订单明细
     */
    @Override
    public OrderItemEntity getById(long id) {
        return orderItemMapper.selectById(id);
    }

    /**
     * 根据批量Id获取订单明细
     *
     * @param batchIds 批量ids
     * @return 订单明细集合
     */
    @Override
    public List<OrderItemEntity> listByBatchIds(List<Long> batchIds) {
        if (batchIds == null || batchIds.isEmpty()) {
            return new ArrayList<>();
        }
        return orderItemMapper.selectBatchIds(batchIds);
    }

    /**
     * 根据批量Id获取订单明细
     *
     * @param orderIds 批量ids
     * @return 订单明细集合
     */
    @Override
    public List<OrderItemEntity> listByBatchOrderIds(List<Long> orderIds) {
        if (orderIds == null || orderIds.isEmpty()) {
            return new ArrayList<>();
        }

        LambdaQueryWrapper<OrderItemEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(OrderItemEntity::getOrderId, orderIds);

        return orderItemMapper.selectList(queryWrapper);
    }

    /**
     * 分页查询
     *
     * @param input input
     * @return 分页的订单明细
     */
    @Override
    public Page<OrderItemEntity> pagedQuery(PagedQueryOrderItemInput input) {
        LambdaQueryWrapper<OrderItemEntity> queryWrapper = Wrappers.lambdaQuery();

        if (input.getMemberId() != null) {
            queryWrapper.eq(OrderItemEntity::getMemberId, input.getMemberId());
        }

        PageHelper.startPage(input.getPageIndex(), input.getPageSize());

        return (Page<OrderItemEntity>) orderItemMapper.selectList(queryWrapper);
    }

    /**
     * 将实体对象转换成domain对象
     *
     * @param orderItemEntity 实体
     * @return orderItem
     */
    @Override
    public OrderItem entityToDomain(OrderItemEntity orderItemEntity) {
        if (orderItemEntity == null) {
            return null;
        }

        OrderItem orderItem = new OrderItem();
        orderItem
                .setId(orderItemEntity.getId())
                .setOrderId(orderItemEntity.getOrderId())
                .setOrderSn(orderItemEntity.getOrderSn())
                .setProductId(orderItemEntity.getProductId())
                .setProductPic(orderItemEntity.getProductPic())
                .setProductName(orderItemEntity.getProductName())
                .setProductBrand(orderItemEntity.getProductBrand())
                .setProductSn(orderItemEntity.getProductSn())
                .setProductPrice(orderItemEntity.getProductPrice())
                .setProductQuantity(orderItemEntity.getProductQuantity())
                .setProductSkuId(orderItemEntity.getProductSkuId())
                .setProductSkuCode(orderItemEntity.getProductSkuCode())
                .setProductCategoryId(orderItemEntity.getProductCategoryId())
                .setSp1(orderItemEntity.getSp1())
                .setSp2(orderItemEntity.getSp2())
                .setSp3(orderItemEntity.getSp3())
                .setPromotionName(orderItemEntity.getPromotionName())
                .setPromotionAmount(orderItemEntity.getPromotionAmount())
                .setCouponAmount(orderItemEntity.getCouponAmount())
                .setIntegrationAmount(orderItemEntity.getIntegrationAmount())
                .setRealAmount(orderItemEntity.getRealAmount())
                .setGiftIntegration(orderItemEntity.getGiftIntegration())
                .setGiftGrowth(orderItemEntity.getGiftGrowth())
                .setProductAttr(orderItemEntity.getProductAttr());

        return orderItem;
    }

    /**
     * 将实体对象转换成domain对象
     *
     * @param orderItemEntities 实体
     * @return orderItems
     */
    @Override
    public List<OrderItem> entityToDomain(List<OrderItemEntity> orderItemEntities) {
        List<OrderItem> result = new ArrayList<>();
        if (orderItemEntities == null) {
            return result;
        }

        for (OrderItemEntity orderItemEntity : orderItemEntities) {
            OrderItem orderItem = entityToDomain(orderItemEntity);
            result.add(orderItem);
        }
        return result;
    }

}
